///|
let chinese_digits_1 = [
  "零", "一", "二", "三", "四", "五", "六", "七", "八", "九",
]

///|
let chinese_units_1 = ["", "十", "百", "千"]

///|
let chinese_sections_1 = ["", "万", "亿", "兆"]

///| 处理四位数内的转换（小写）
fn convert_section_1(num : Int) -> String {
  if num == 0 {
    return ""
  }
  let mut result = ""
  let mut temp = num

  // 处理千位
  let qian = temp / 1000
  if qian > 0 {
    result = result + chinese_digits_1[qian] + chinese_units_1[3]
  }
  temp = temp % 1000

  // 处理百位
  let bai = temp / 100
  if bai > 0 {
    result = result + chinese_digits_1[bai] + chinese_units_1[2]
  } else if qian > 0 && temp > 0 {
    result = result + chinese_digits_1[0]
  }
  temp = temp % 100

  // 处理十位
  let shi = temp / 10
  if shi > 1 {
    result = result + chinese_digits_1[shi] + chinese_units_1[1]
  } else if shi == 1 {
    result = result + chinese_units_1[1]
  } else if (qian > 0 || bai > 0) && temp > 0 {
    result = result + chinese_digits_1[0]
  }
  temp = temp % 10

  // 处理个位
  if temp > 0 {
    result = result + chinese_digits_1[temp]
  }
  result
}

///| 将数字转换为中文小写形式，支持小数。
///
/// 参数：
///
/// * `amount`：要转换为中文小写格式的数字（支持小数）。
///
/// 返回包含该数字中文小写表示的字符串。
///
/// 示例：
///
/// ```moonbit
/// inspect(Convert::to_chinese_digital_full(134.45), content="一百三十四点四五")
/// inspect(Convert::to_chinese_digital_full(0.05), content="零点零五")
/// inspect(Convert::to_chinese_digital_full(-123.4), content="负一百二十三点四")
/// ```
///
pub fn Convert::to_chinese_digital_full(amount : Double) -> String {
  if amount == 0.0 {
    return chinese_digits_1[0]
  }
  let sign = if amount < 0.0 { "负" } else { "" }
  let abs_amount = amount.abs()
  let integer_part = abs_amount.to_int()
  let mut result = sign + Convert::to_chinese_digital(integer_part)

  //let decimal_part = (abs_amount - integer_part.to_double()).to_string().substring(start=2)  // 获取小数部分字符串

  let index = match amount.to_string().find(".") {
    Some(i) => i
    None => return result
  }
  // 小数部分
  let decimal_part = amount
    .to_string()
    .substring(start=index + 1)
    .trim_end("0")
    .to_string()

  if decimal_part.length() > 0 {
    result = result + "点"
    for i = 0; i < decimal_part.length(); i = i + 1 {
      let digit = Convert::string_to_int(
        decimal_part.substring(start=i, end=i + 1),
      ).unwrap()
      result = result + chinese_digits_1[digit]
    }
  }
  result
}

// 单元测试

///|
test "test_to_chinese_digital" {
  assert_true(Convert::to_chinese_digital(123) == "一百二十三")
  assert_true(Convert::to_chinese_digital(1001) == "一千零一")
  assert_true(Convert::to_chinese_digital(10001) == "一万零一")
  assert_true(Convert::to_chinese_digital(0) == "零")
  assert_true(Convert::to_chinese_digital(-456) == "负四百五十六")
}

///|
test "test_to_chinese_digital_full" {
  assert_true(Convert::to_chinese_digital_full(134.45) == "一百三十四点四五")
  assert_true(Convert::to_chinese_digital_full(0.05) == "零点零五")
  assert_true(Convert::to_chinese_digital_full(-123.4) == "负一百二十三点四")
  assert_true(Convert::to_chinese_digital_full(100.0) == "一百")
  assert_true(Convert::to_chinese_digital_full(0.0) == "零")
}

///| 将整数转换为中文小写形式。
///
/// 参数：
///
/// * `num`：要转换为中文小写格式的整数。
///
/// 返回包含该数字中文小写表示的字符串。
///
/// 示例：
///
/// ```moonbit
/// inspect(@lib.Convert::to_chinese_digital(123), content="一百二十三")
/// inspect(@lib.Convert::to_chinese_digital(1001), content="一千零一")
/// inspect(@lib.Convert::to_chinese_digital(10001), content="一万零一")
/// ```
///
pub fn Convert::to_chinese_digital(num : Int) -> String {
  if num == 0 {
    return chinese_digits_1[0]
  }

  if num < 0 {
    return "负" + Convert::to_chinese_digital(-num)
  }

  let mut sections = []
  let mut temp = num
  while temp > 0 {
    sections = [temp % 10000] + sections
    temp = temp / 10000
  }

  let mut result = ""
  let sections_len = sections.length()
  for i = 0; i < sections_len; i = i + 1 {
    let section = sections[i]
    let section_index = sections_len - 1 - i

    if section > 0 {
      let section_str = convert_section_1(section)
      if section_str.length() > 0 {
        // 检查是否需要在前面添加零
        if i > 0 && section < 1000 {
          let mut has_prev_content = false
          for j = 0; j < i; j = j + 1 {
            if sections[j] > 0 {
              has_prev_content = true
              break
            }
          }
          if has_prev_content {
            result = result + chinese_digits_1[0]
          }
        }
        result = result + section_str + chinese_sections_1[section_index]
      }
    }
  }

  // 清理多余的零
  while result.contains("零零") {
    result = result.replace(old="零零", new="零")
  }
  if result.has_prefix("零") {
    result = result.substring(start=1)
  }
  if result.has_suffix("零") {
    result = result.substring(end=result.length() - 1)
  }
  result
}

///|
fn _convert_section_1_(num : Int) -> String {
  if num == 0 {
    return ""
  }
  let mut result = ""
  let mut temp = num

  // 处理千位
  let qian = temp / 1000
  if qian > 0 {
    result = result + chinese_digits[qian] + chinese_units[3]
  }
  temp = temp % 1000

  // 处理百位
  let bai = temp / 100
  if bai > 0 {
    result = result + chinese_digits[bai] + chinese_units[2]
  } else if qian > 0 && temp > 0 {
    result = result + chinese_digits[0]
  }
  temp = temp % 100

  // 处理十位
  let shi = temp / 10
  if shi > 0 {
    result = result + chinese_digits[shi] + chinese_units[1]
  } else if (qian > 0 || bai > 0) && temp > 0 {
    result = result + chinese_digits[0]
  }
  temp = temp % 10

  // 处理个位
  if temp > 0 {
    result = result + chinese_digits[temp]
  }
  result
}

///|
pub fn Convert::to_chinese_upper_(num : Int) -> String {
  if num == 0 {
    return chinese_digits[0]
  }
  if num < 0 {
    return "负" + Convert::to_chinese_upper_(-num)
  }
  let mut sections = []
  let mut temp = num
  while temp > 0 {
    sections = [temp % 10000] + sections
    temp = temp / 10000
  }
  let mut result = ""
  let sections_len = sections.length()
  for i = 0; i < sections_len; i = i + 1 {
    let section = sections[i]
    let section_index = sections_len - 1 - i
    if section > 0 {
      let section_str = convert_section(section)
      if section_str.length() > 0 {
        // 检查是否需要在前面添加零
        if i > 0 && section < 1000 {
          let mut has_prev_content = false
          for j = 0; j < i; j = j + 1 {
            if sections[j] > 0 {
              has_prev_content = true
              break
            }
          }
          if has_prev_content {
            result = result + chinese_digits[0]
          }
        }
        result = result + section_str + chinese_sections[section_index]
      }
    }
  }

  // 清理多余的零
  while result.contains("零零") {
    result = result.replace(old="零零", new="零")
  }
  if result.has_prefix("零") {
    result = result.substring(start=1)
  }
  if result.has_suffix("零") {
    result = result.substring(end=result.length() - 1)
  }
  result
}
